package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// 优惠码使用日志
type CouponLog struct {
	ID         string `gorm:"primaryKey;column:id;type:uint;size:64;comment:唯一ID" json:"id"`
	BatchID    string `gorm:"column:batch_id;type:bigint;comment:优惠码活动ID" json:"batch_id"`
	Code       string `gorm:"column:code;type:varchar(10);comment:优惠码" json:"code"`
	OutTradeNo string `gorm:"column:out_trade_no;type:string;size:30;comment:订单交易号" json:"out_trade_no"`
	MerchantID string `gorm:"column:merchant_id;type:int;size:64;comment:商户id" json:"merchant_id"`
	CreatedAt  int64  `gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间" json:"created_at"`
}

func (m CouponLog) TableName() string {
	return "coupon_log"
}

// BeforeCreate 自增id
func (cl *CouponLog) BeforeCreate(tx *gorm.DB) (err error) {
	if cl.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	cl.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &CouponLog{})
}
